package delta;

import java.util.ArrayList;

public class Obstacle implements IFieldable {

	private Double speedMultiplier;

	private Road place;

	private ArrayList<MagicStoneToObstacle> magicStones;

	public String toString() {
		StringBuilder str = new StringBuilder();
		str.append("Obstacle:\n");
		str.append("speedMultiplier: " + speedMultiplier + "\n");
		str.append("Number of MagicStone: " + magicStones.size() + "\n");
		str.append("\n");
		return str.toString();

	}

	public Boolean addMagicStone(MagicStoneToObstacle ms) {
		if (magicStones.size() > 3) {
			return false;
		} else {
			magicStones.add(ms);
			speedMultiplier *= ms.getSpeedMultiplier();
			return true;
		}
	}

	public void update() {
		ArrayList<IFieldable> objects = place.getObjects();
		for (IFieldable iFieldable : objects) {
			iFieldable.action(new ActionParameters(magicStones.size() + 2));
		}
	}

	public Obstacle(Field road) {
		//=============================================================
		//A view irasakor kerult bele
		//=============================================================
		place=null;
		//Obstacle csak akkor johet letre ha utra tettek
		if(road!=null && road.getDistance()!=-1){
			//csak akkor johet letre ha a helyen meg nincs obstacle
			boolean van_obstacle=false;
			for(IFieldable obj: road.getObjects()){
				//true -val ter vissza ha enemy
				//Tower pedig "ugyse" lesz mert ekkor mar kiderult hogy utrol van szo
				if(!obj.action(null)){
					van_obstacle=true;
					break;
				}
			}
			if(!van_obstacle){
				//Ha nincs akadaly, le lehet rakni egy ujat
				this.place = (Road)road;
				this.speedMultiplier = 1.0;
				this.magicStones = new ArrayList<MagicStoneToObstacle>();
				road.addObject(this);
				Player.decreaseWitchcraft(25);
			}
				
		}
		
		

	}

	public boolean action(ActionParameters ap) {
		return false;
	}

	@Override
	public String getMapchar() {
		
		return new String("O");
		
	}

	@Override
	public Field getField() {
		// TODO Auto-generated method stub
		return place;
	}

}